/* $Header: p_filter.h,v 2.2 01/10/02 10:30:33 przybyls Exp $ */

/***********************************************************************/
/* INCLUDE FILE NAME: p_filter.h                                       */
/* ==================                                                  */
/*                                                                     */
/* Author: wikfors                                                     */
/*                                                                     */
/* COMMENT: provide macros for filtering setpts and actuals            */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

#define AUX_PRES_FILTER_RATE  8
#define AUX_PRES_DELAY       10

#ifndef P_FILTER_H
#define P_FILTER_H

/*****************************/
/*   IFDEFS                  */
/*****************************/


/*****************************/
/*   SYMBOLIC CONSTANTS      */
/*****************************/


/*****************************/
/*   MACROS                  */
/*****************************/


#define FILTER_MODULO  8
#define FILTER_SHIFT   3


#define FilterValue(FILTER,VALUE)                                                   \
            if ( (FILTER) < (VALUE))                                                \
            {                                                                       \
                 if ( ((VALUE) - (FILTER)) > FILTER_MODULO )                        \
                 {                                                                  \
                      (FILTER) += (((VALUE) - (FILTER)) >> FILTER_SHIFT);           \
                 }                                                                  \
                 else                                                               \
                 {                                                                  \
                      (FILTER)++;                                                   \
                 }                                                                  \
            }                                                                       \
            else if ( (FILTER) > (VALUE))                                           \
            {                                                                       \
                 if ( ((FILTER) - (VALUE)) > FILTER_MODULO )                        \
                 {                                                                  \
                      (FILTER) -= (((FILTER) - (VALUE)) >> FILTER_SHIFT);           \
                 }                                                                  \
                 else                                                               \
                 {                                                                  \
                      (FILTER)--;                                                   \
                 }                                                                  \
             }                                                                      \

#define JamFilter(FILTER,VALUE)  (FILTER) = (VALUE);



#define NewFilterValue(FILTER,VALUE,DIVISOR)                                        \
            if ( (FILTER) < (VALUE))                                                \
            {                                                                       \
                 if ( ((VALUE) - (FILTER)) > DIVISOR )                              \
                 {                                                                  \
                      (FILTER) += (((VALUE) - (FILTER)) / DIVISOR );                \
                 }                                                                  \
                 else                                                               \
                 {                                                                  \
                      (FILTER)++;                                                   \
                 }                                                                  \
            }                                                                       \
            else if ( (FILTER) > (VALUE))                                           \
            {                                                                       \
                 if ( ((FILTER) - (VALUE)) > DIVISOR )                              \
                 {                                                                  \
                      (FILTER) -= (((FILTER) - (VALUE)) / DIVISOR );                \
                 }                                                                  \
                 else                                                               \
                 {                                                                  \
                      (FILTER)--;                                                   \
                 }                                                                  \
             }                                                                      \



#if 0


#define REMAINDER_BITS   8  /* bits to save any remainders */
#define APPROACH_SLOPE   8
#define NOISE_THRESHOLD 16



#define LongTimeFilterValue(FILTER,VALUE)                                                   \
            if ( (FILTER) < ((VALUE)<<REMAINDER_BITS) )                                      \
            {  /* let the filter grow */                                                    \
                if (( (((VALUE)<<REMAINDER_BITS) - (FILTER)) / APPROACH_SLOPE ) > (NOISE_THRESHOLD<<REMAINDER_BITS)) \
                {                                                                           \
                      (FILTER) += (((VALUE)<<REMAINDER_BITS) - (FILTER)) / APPROACH_SLOPE;   \
                }                                                                           \
                else                                                                        \
                      (FILTER)++;                                                           \
            }                                                                               \
            else if ( (FILTER) > ((VALUE)<<REMAINDER_BITS) )                                 \
            {  /* make the filter decrease */                                               \
                if (( ((FILTER) - ((VALUE)<<REMAINDER_BITS)) / APPROACH_SLOPE ) > (NOISE_THRESHOLD<<REMAINDER_BITS)) \
                {                                                                           \
                      (FILTER) -= ((FILTER) - ((VALUE)<<REMAINDER_BITS)) / APPROACH_SLOPE;   \
                }                                                                           \
                else                                                                        \
                      (FILTER)--;                                                           \
            }                                                                               \



#define LongTimeJamFilter(FILTER,VALUE)  (FILTER) = ((VALUE)<<REMAINDER_BITS)
     
#define GetLongTimeFilteredValue(FILTER)    ((FILTER)>>REMAINDER_BITS)



#else

#define LongTimeFilterValue(FILTER,VALUE)                                           \
                                                                                    \
              FilterValue((FILTER),(VALUE))                                         \
                                                                                    \

#define LongTimeJamFilter(FILTER,VALUE)                                             \
                                                                                    \
              JamFilter((FILTER),(VALUE))                                           \
                                                                                    \
     
#define GetLongTimeFilteredValue(FILTER)                                            \
                                                                                    \
                              ((FILTER))                                            \
                                                                                    \

#endif



/*****************************/
/*   TYPEDEFS                */
/*****************************/


/*****************************/
/*   FUNCTION PROTOTYPES     */
/*****************************/

#endif
